package com.gzck.gzck_verson2.controller;

import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzck.gzck_verson2.pojo.GFwdUserinfo;
import com.gzck.gzck_verson2.pojo.GFwdUserprivilege;
import com.gzck.gzck_verson2.pojo.vo.AddProjectVO;
import com.gzck.gzck_verson2.service.IGFwdUserinfoService;
import com.gzck.gzck_verson2.service.IGFwdUserprivilegeService;
import com.gzck.gzck_verson2.service.IGProjectUserService;
import com.gzck.gzck_verson2.service.impl.GFwdUserinfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    IGFwdUserinfoService UserinfoService;
    @Autowired
    IGFwdUserprivilegeService userPrivilegeService;
    //用户工程中间表
    @Autowired
    IGProjectUserService igProjectUserService;

    // 测试登录，浏览器访问： http://localhost:8080/user/doLogin?username=qqqqqq&password=123456

    /**
     * 用户登录
     */
    @GetMapping("doLogin")
    public SaResult doLogin(String username, String password) {
        //密码加密
        String md5 = SecureUtil.md5(password);
        GFwdUserinfo user = UserinfoService.getOne(
                Wrappers.lambdaQuery(GFwdUserinfo.class)
                        .eq(GFwdUserinfo::getUsername, username)
                        .eq(GFwdUserinfo::getPwd, md5)
        );
        if (ObjectUtil.isEmpty(user)) {
            return SaResult.error("用户名或密码错误");
        } else if (user.getPower().equals("1")) {
            return SaResult.ok("管理员登陆成功");
        } else if (user.getPower().equals("2")) {
            return SaResult.ok("普通用户登陆成功");
        }
        return SaResult.error("用户权限错误");
    }


    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("doRegister")
    public SaResult doRegister(@RequestBody GFwdUserinfo user) {
        GFwdUserinfo userinfo = UserinfoService.getOne(
                Wrappers.lambdaQuery(GFwdUserinfo.class)
                        .eq(GFwdUserinfo::getUsername, user.getUsername())
        );
        if (ObjectUtil.isNotEmpty(userinfo)) {
            return SaResult.error("该用户已存在");
        }
        user.setPwd(SecureUtil.md5(user.getPwd()));

        UserinfoService.save(user);
        GFwdUserinfo userinfo2 = UserinfoService.getOne(
                Wrappers.lambdaQuery(GFwdUserinfo.class)
                        .eq(GFwdUserinfo::getUsername, user.getUsername())
        );
        if (ObjectUtil.isNotEmpty(userinfo2)) {
            GFwdUserprivilege gFwdUserprivilege = GFwdUserprivilege.builder().userid(userinfo2.getId()).privilege(userinfo2.getPower()).build();
            userPrivilegeService.save(gFwdUserprivilege);
        }
        return SaResult.ok("注册成功");
    }


    // 查询登录状态，浏览器访问： http://localhost:8080/user/isLogin
    @RequestMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录" + StpUtil.isLogin());
    }

    //查询token信息  http://localhost:8080/user/tokeninfo
    @GetMapping("tokeninfo")
    public SaResult tokeninfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    //强制下线  http://localhost:8080/user/logout
    @GetMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok("退出成功");
    }


    //为用户添加对应的工程
    //格式
    // {
    //  "userId": 123456789,
    //  "projectIds": [987654321, 456789012, 123012345]
    //}

    @PostMapping("/saveData")
    public SaResult saveData(@RequestBody AddProjectVO requestData) {
        // 处理接收到的数据，AddProjectVO是用于映射JSON数据的类
        igProjectUserService.save(requestData);
        return SaResult.ok("添加成功");
        // 假设处理成功，返回一个表示成功的响应
    }
}
